<%@ include file="../import_page.jsp" %>
<%
	//login checking
	if(!loginedChecking(session, UsersType.student)) {
		//user don't login
		//redirect to home page
		response.sendRedirect(URL_HOMEPAGE);
		return;
	}
	
	//select unit check
	if(!selectUnitCheck(session)) {
		//if user didn't select an Unit
		//redirtect to select page
		response.sendRedirect(URL_SELECT_UNIT);
		return;
	}
	
	Users loginUser = (Users)session.getAttribute(SESSION_LOGIN_USER);
	Units selectUnit = (Units)session.getAttribute(SESSION_SELECT_UNIT);
	String errMsg = null;
	
	ArrayList<AssignmentCollector> listAss = null;
	//get assignment
	try {
		AssignmentsControl assCon = new AssignmentsControl();
		listAss = assCon.getAssignments(selectUnit.getId());
		assCon.closeDBConnection();
	} catch (Exception err) {
		errMsg = err.toString();
	}
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Uni-Learn - Assignment Select</title>
<style type="text/css">
<!--
.style1 {	font-size: large;
	font-weight: bold;
}
.style2 {font-size: xx-large}
-->
</style>
</head>

<body>
<p>Unit: <%=selectUnit.getName() %> (<%=selectUnit.getId() %>)</p>
<% if(!isEmptyString(errMsg)) {%>
<h1><%=errMsg %></h1>
<%}%>
<table width="100%" border="1" align="center" bordercolor="#CCCCCC" bgcolor="#00FF99">
  <tr>
    <th colspan="6"><div align="center"><span class="style1">Assignment Selection </span></div></th>
  </tr>
  <tr>
  	<th>Name</th>
  	<th>Individual / Group</th>
  	<th>Deadline</th>
  	<th>File</th>
  	<th>Status</th>
  	<th>Mark</th>
  </tr>
<%
	if(listAss != null) {
		Iterator<AssignmentCollector> laI = listAss.iterator();
		ResultControl rCon = new ResultControl();
		while(laI.hasNext()) {
			AssignmentCollector ac = laI.next();
			AssignmentResult ar = null;
			try {
				ar = rCon.getBriefResult(ac.getId(), loginUser.getId());
			} catch (Exception err) {
				//error message ignore, because the result of the assignment may not found (student do not submit yet)
			}
%>
   <tr>
    <td height="63">
    	<span class="style2"><a href="#"><%= ac.getName() %></a></span>
    </td>
    <td><%= (ac.getGroupSize() < 2 ? "Individual" : ("Group (" + ac.getGroupSize()+ ")")) %></td>
    <td><%= formatDateTime(ac.getDeadline()) %></td>
<%
			String fileString = "(None)";
			if(ar != null && !isEmptyString(ar.getFileName())) {
				fileString = "<a href=\""+ addParmeterToUrl(URL_DOWNLOAD_ASSIGNMENT, PAR_ASSIGNMENT_ID, Integer.toString(ar.getAcId())) +"\">"+ ar.getFileName()+ "</a>";
			}
%>
    <td><%= fileString %></td>
    <td><%= (ar == null) ? "unsubmit" : ar.getStatus().toString() %></td>
<%
			String markStr;
			if(ar == null || ar.getStatus() == SubmitStatus.unsubmit) {
				//print 0 mark
				markStr = "0 / "+ ac.getFullMark().toString();
				if(ac.isSubmission()) {
					//print submit button
					markStr += "<br /><a href=\""+ addParmeterToUrl(URL_UPLOAD_ASSIGNMENT, PAR_ASSIGNMENT_ID, Integer.toString(ac.getId())) +"\">Submit Now</a>";
				} else {
					//print cannot submit
					markStr += "<br />Submit not available";
				}
			} else {
				markStr = "<a href=\""+ addParmeterToUrl(URL_VIEW_DETAIL_RESULT, PAR_ASSIGNMENT_ID, Integer.toString(ac.getId()))+ "\">" + ar.getMark().toString() + " / "+ ac.getFullMark().toString()+ "</a>"; 
			}
%>
	<td><%=markStr %></td>
  </tr>
  <tr>
    	<td colspan="6">
    	<table width="90%" border="1" bordercolor="#CCCCCC" style="margin: 0 5%;">
    		<tr>
    			<td>Information:</td>
    			<td><%= muiliteLineString(ac.getInfo()) %></td>
    		</tr>
    		<tr>
    			<td>Material:</td>
    			<td>
    			<table width="100%" border="1" >
    				<tr>
    					<td>
    					Material 1<br />Material 1 information<br /><a href="#">Download: Material 1</a>
    					</td>
    				</tr>
    				<tr>
    					<td>Material 1<br />Material 1 information<br /><a href="#">Download: Material 1</a></td>
    				</tr>
    			</table>
    			</td>
    		</tr>
    	</table>
    	</td>
  
  </tr>
<%
		}
		rCon.closeDBConnection();
	}
%>
</table>
</body>
</html>
